package pattern2.GuardedSuspension.demo;

/**
 * @创建人 liweijia
 * @创建时间 2022/7/23
 * @描述 程序的主要用途
 */
public class Sender extends Thread {

    /**
     * 锁
     */
    private final Object lock;

    /**
     *
     */
    private final Receiver receiver;

    Sender(Object lock, Receiver receiver) {
        this.lock = lock;
        this.receiver = receiver;
    }

    @Override
    public void run() {
        synchronized (lock) {
            System.out.println("发送者：正在制作消息");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            Msg msg = new Msg();
            msg.setContent("这是一条消息");
            System.out.println("发送者：消息制作完毕，发送给接收者。");
            receiver.setMsg(msg);
            lock.notifyAll();
        }
    }
}
